package com.sealer.pattern.creational.prototype;

/**
 * Created by sealer on 17/04/01.
 */
public class DeepClone {
    public static void main(String[] args) {
        Apple a = new Apple();
        a.setWeight(9);
        a.setSkin(new Skin("red"));
        Apple b = a.clone();

        a.getSkin().setColor("green");
        a.setWeight(12);
        System.out.println(a);
        System.out.println(b);
    }
}

class Apple implements Cloneable {
    private int weight;
    private Skin skin;

    public int getWeight() {
        return weight;
    }

    public void setWeight(int weight) {
        this.weight = weight;
    }

    public Skin getSkin() {
        return skin;
    }

    public void setSkin(Skin skin) {
        this.skin = skin;
    }

    public Apple clone() {
        Apple apple = null;
        try {
            apple = (Apple) super.clone();
            apple.setSkin(apple.getSkin().clone());
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }

        return apple;
    }

    @Override
    public String toString() {
        return "Apple{" +
                "weight=" + weight +
                ", skin=" + skin +
                '}';
    }
}

class Skin implements Cloneable {
    private String color;

    public Skin(String color) {
        this.color = color;
    }

    public Skin() {
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public Skin clone() {
        Skin skin = null;
        try {
            skin = (Skin) super.clone();
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
        return skin;
    }

    @Override
    public String toString() {
        return "Skin{" +
                "color='" + color + '\'' +
                '}';
    }
}